package com.ruoyi.web.controller.works;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SysOrganization;
import com.ruoyi.system.domain.Work;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.WorkService;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.security.SecurityUtil;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author 柳
 * @Date 2021/3/5
 * @Description TODO
 */
@RestController
@RequestMapping("/work")
public class WorkController {
    @Autowired
    private WorkService workService;

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysRoleService sysRoleService;

    @ApiOperation("根据赛事id查询是否有大众评审作品")
    @GetMapping("/queryReviewWorkByEpid/{epid}")
    public AjaxResult queryReviewWorkByEpid(@PathVariable Integer epid) {
        if(epid==0||epid==null){
            return AjaxResult.error();
        }
        List<Work> workList = workService.queryReviewWorkByEpid(epid);
        if (workList.size()==0||workList==null){
            return AjaxResult.error("该赛事下没有大众评审作品");
        }
        return AjaxResult.success();
    }


    @ApiOperation("根据赛事id查询作品信息(填充下拉框)")
    @GetMapping("/selectWorkByEpid/{epid}")
    public AjaxResult selectWorkByEpid(@PathVariable Integer epid) {
        if(epid==0||epid==null){
            return AjaxResult.error();
        }
        List<Work> workList = workService.selectWorkByEpid(epid);
        return AjaxResult.success(workList);
    }


    @ApiOperation("增加大赛作品投票数")
    @PutMapping("/addVoteCount")
    public AjaxResult addVoteCount(@RequestBody List<Work> workList) {
        if(workList.size()==0 || workList==null){
            return AjaxResult.error();
        }
        for (Work work : workList) {
            workService.addVoteCount(work);
        }
        return AjaxResult.success();
    }


    @PutMapping("/updateReviewStateByWorkId")
    @ApiOperation("改变作品状态为大众评审作品")
    public AjaxResult updateReviewStateByWorkId(@RequestBody List<String> ids) {
        for (String id : ids) {
            workService.updateReviewStateByWorkId(id);
        }
        return AjaxResult.success();
    }



    @GetMapping("/queryWorksByTid")
    @ApiOperation("教师端根据教师id查询待评审作品信息")
    public AjaxResult queryWorksByTid(Work work) {
        PageHelper.startPage(work.getCurrentPage(),work.getPageSize());
        List<Work> workList = workService.queryWorksByTid(work);
        PageInfo<Work>  pageInfo=new PageInfo<>(workList);
        return AjaxResult.success(pageInfo);
    }


    @GetMapping("/queryWorkByEpid/{epid}")
    @ApiOperation("/根据大赛id查询作品信息")
    public AjaxResult queryWorkByEpid(@PathVariable Integer epid) {
        List<Work> workList = workService.queryWorkByEpid(epid);
        return AjaxResult.success(workList);
    }

    @GetMapping("/queryPublicReviewWorkByEpid")
    @ApiOperation("/根据大赛id查询参加大众评审的作品信息")
    public AjaxResult queryPublicReviewWorkByEpid(Integer epid, String event_step) {
        List<Map<String, Object>> workList = workService.queryPublicReviewWorkByEpid(epid, event_step);
        return AjaxResult.success(workList);
    }



    @GetMapping("/queryWorks")
    @ApiOperation("/查询所有大赛作品信息")
    public AjaxResult queryWorks(Work work) {
        //获取当前登录者的账户
        SysUser sysUser = sysUserService.selectUserById(Long.parseLong(work.getUser_id()));
        boolean isAdmin = false; //是否为管理员
        if (sysUser!= null ) {
            //获取当前用户的角色
            List<SysRole> roles = sysUser.getRoles();;
            if (roles != null) {
                for (SysRole role : roles) {
                    //如果当前用户有管理员角色
                    if ("common".equals(role.getRoleKey()) ) {
                        isAdmin = true;
                        break;
                    }
                }
            }
        }
        List<Work> workList = null;
        PageInfo<Work>  pageInfo=null;
        PageHelper.startPage(work.getCurrentPage(),work.getPageSize());
        if (isAdmin) { //如果是管理员要查看所有的作品
            workList = workService.queryWorksForAdmin(work);
            pageInfo=new PageInfo<>(workList);
        } else { //如果不是管理员查看当前用户所负责的赛事作品
            workList = workService.queryWorks(work);
            pageInfo=new PageInfo<>(workList);
        }
        return AjaxResult.success(pageInfo);
    }

    @GetMapping("/selectWorkBySid/{sid}")
    @ApiOperation("/根据用户sid查询参赛作品")
    public AjaxResult selectWorkBySid(@PathVariable String sid) {
        List<Work> workList = workService.selectWorkBySid(sid);
        return AjaxResult.success(workList);
    }
}
